const http = require('http');
const server = http.createServer();
server.listen(3000,()=>{
  console.log('服务器已启动');
});
const fs = require('fs');
const path = require('path');
const url = require('url');
//4.监听浏览器请求
//监听2个url地址：
// /get -- get.html  和 /getData -- 接收表单提交的数据
//get方式传递数据时url地址的格式？
// /getData?key1=value1&key2=value2&...
server.on('request',(req,res)=>{
  // const url = req.url;
  const urlObj = url.parse(req.url,true);
  // console.log(urlObj)
  if(urlObj.pathname === '/get'){
  //显示get.html页面
    fs.readFile(path.join(__dirname,'view/get.html'),(err,data)=>{
      if(err){
        console.log(err);
        return res.end('404 not found');
      }
      res.end(data);
    })
  }else if(urlObj.pathname === '/getData'){
    //接收get表单提交的数据
    console.log(urlObj.query);
    res.end('ok');
  }else if(urlObj.pathname === '/aaa'){
    console.log(urlObj.query);
    res.end('/aaa-ok')
  }else if(urlObj.pathname === '/bbb'){
    console.log(urlObj.query);
    res.end('/bbb-ok');
  }
})